package com.igirl.web.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.igirl.common.service.ApiService;
import com.igirl.web.service.PropertiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.*;

/**
 * @Author: jiaheng
 * @Description:
 * @Date: Created in 2018/1/2
 * @Modified:
 */
@Service
public class AdService {

    @Autowired
    private ApiService apiService;

    @Autowired
    private PropertiesService propertiesService;

    public String queryBigAd() throws URISyntaxException, IOException {
        String url = propertiesService.manageUrl + propertiesService.AD1Url;
        String result = this.apiService.doGet(url);
        JsonNode jsonNode = PropertiesService.MAPPER.readTree(result);
        ArrayNode arrayNode = (ArrayNode) jsonNode.get("rows");
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (JsonNode node : arrayNode) {
            Map<String, Object> map = new HashMap<>();
            map.put("srcB", node.get("pic2"));
            map.put("height", 240);
            map.put("alt", node.get("title"));
            map.put("width", 670);
            map.put("src", node.get("pic"));
            map.put("widthB", 550);
            map.put("href", node.get("url"));
            map.put("heightB", 240);
            mapList.add(map);
        }
        return PropertiesService.MAPPER.writeValueAsString(mapList);
    }

    public String queryleftUpAd() throws IOException {
        String url = propertiesService.manageUrl + propertiesService.AD2Url;
        String result = this.apiService.doGet(url);
        JsonNode jsonNode = PropertiesService.MAPPER.readTree(result);
        ArrayNode arrayNode = (ArrayNode) jsonNode.get("rows");
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (JsonNode node : arrayNode) {
            Map<String,Object> map = new HashMap<String,Object>();
            String title = node.get("title").asText();
            String imgUrl = node.get("url").asText();
            String imgPath = node.get("pic").asText();
            map.put("srcB", imgPath);
            map.put("height", 70);
            map.put("alt",title );
            map.put("width",310 );
            map.put("src", imgPath);
            map.put("widthB",210 );
            map.put("href", imgUrl);
            map.put("heightB",70 );
            mapList.add(map);
        }
        return PropertiesService.MAPPER.writeValueAsString(mapList);
    }
}
